package com.zf.sqlite.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import java.io.File;

@Database(entities = {ZFEDLogsEntity.class, WhitelistEntity.class, BlacklistEntity.class}, version = 4, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
    public abstract ZFEDLogsDao zfedLogsDao();
    public abstract WhitelistDao whitelistDao();
    public abstract BlacklistDao blacklistDao();

    private static volatile AppDatabase INSTANCE;

    // 添加从版本1到版本2的迁移
    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            // 由于表结构在版本1中不存在，我们需要创建表
            database.execSQL("CREATE TABLE IF NOT EXISTS `zfed_logs` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `user` TEXT, `log_info` TEXT, `log_result` TEXT, `log_time` TEXT)");
            database.execSQL("CREATE TABLE IF NOT EXISTS `zfed_whitelist` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `type` INTEGER NOT NULL, `device_name` TEXT, `device_mac` TEXT, `device_manufacturer` TEXT, `whitelist_add_time` TEXT)");
        }
    };

    // 添加从版本2到版本3的迁移
    static final Migration MIGRATION_2_3 = new Migration(2, 3) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            // 为zfed_whitelist表添加remarks字段
            database.execSQL("ALTER TABLE zfed_whitelist ADD COLUMN remarks TEXT");
        }
    };

    // 添加从版本3到版本4的迁移
    static final Migration MIGRATION_3_4 = new Migration(3, 4) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            // 创建黑名单表
            database.execSQL("CREATE TABLE IF NOT EXISTS `zfed_backlist` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `type` INTEGER NOT NULL, `device_name` TEXT, `device_mac` TEXT, `device_manufacturer` TEXT, `frequency_point` REAL, `frequency_band_id` INTEGER, `fm_audio_frequency` REAL, `fm_video_frequency` REAL, `backlist_add_time` TEXT, `remarks` TEXT)");
        }
    };

    public static AppDatabase getInstance(Context context) {
        // 创建数据库文件路径
        File databaseDir = new File(Environment.getExternalStorageDirectory(), "database");
        File databaseFile = new File(databaseDir, "zfed.db");

        // 检查并创建目录
        if (!databaseDir.exists()) {
            boolean dirCreated = databaseDir.mkdirs();
            if (!dirCreated) {
                return null;
            }
            Log.d("AppDatabase", "Database directory created successfully");
            Log.e("AppDatabase", databaseFile.getAbsolutePath());
            Log.e("AppDatabase", databaseFile.getAbsolutePath());
        } else {
            Log.d("AppDatabase", "database directory already exists");
        }
        synchronized (AppDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(
                        context.getApplicationContext(),
                        AppDatabase.class, databaseFile.getAbsolutePath()
                )
                .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4) // 添加迁移对象
                .build();
            }
        }
        return INSTANCE;
    }
}